#!/bin/bash
# $Id: lookfor-sd-devs.sh 635 2010-01-17 17:15:08Z ant_code $

# desc: output /var/tmp/usb_sd[a-z][0-9] as text file, in it desc and size 
# 			of the device, and to stdout

export syspath=/sys/block
# ex. usblist="sda1 sdb"
export usblist=""
export tmpmnt="$(mktemp -d /tmp/tmp.XXXXXX)"

# notes: needs lsscsi
find_dev ()
{
	if [ -d /mostaza ]; then
		mdev -s
	fi

	local n

	for b in  $(lsscsi -H|grep usb-storage|tr -d "[]"|sed 's/usb-storage//g'|tr "\n" " ") ; do
		d[$((++n))]="$(lsscsi |grep \\[$b |awk '{print $NF}')"
	done

	local l
	
	for l in ${d[*]} ; do
		echo $l
		usblist="${usblist} $(_test_mount $(basename $l))"
	done

	if [ -z "$usblist" ]; then
		return 1
	else
		echo $usblist
	fi
}

# param: 1:sdX
_test_mount () 
{
	local l=$1
	for n in 0 1 2 3 4 5 6 7 8 9; do
		mount -o ro /dev/${l}${n/0/\ } $tmpmnt >/dev/null 2>&1
		if [ "$?" -eq "0" ]; then 
			echo ${l}${n/0/\ }
		fi
		umount /dev/${l}${n/0/\ } $tmpmnt >/dev/null 2>&1
	done
}

id_dev ()
{
	local V
	local M
	local S
	local tmpdir="/var/tmp"

	for u in $usblist; do
		V="$(< $syspath/$(echo $u|tr -d [1-9])/device/vendor)"	
		M="$(< $syspath/$(echo $u|tr -d [1-9])/device/model)"	
		S="$(fdisk -l /dev/${u} 2>/dev/null |grep -E "GB|MB" |cut -f2 -d":"|cut -f1 -d",")"
		#
		echo "Device: $u" > $tmpdir/usb_${u}
		echo "Brand: $V" >> $tmpdir/usb_${u}
		echo "Model: $M" >> $tmpdir/usb_${u}
		echo "Size: ${S}" >> $tmpdir/usb_${u}
	done
}

find_dev && id_dev

# vim: ts=2 bs=2
# lookfor-sd-devs.sh: end
